package com.ai;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/09/06/星期六 10:52
 * @Component: 成都蜗牛学苑
 **/
public class MyService {
    public static void main(String[] args) {
        //字符串也是对象，所以可以用来加锁，但是工作中不能这么用
        new Thread(()->{
            synchronized ("A"){
                System.out.println("A线程获取到了A锁");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized ("B"){
                    System.out.println("A线程获取到了B锁");
                }
            }
        },"A").start();
        new Thread(()->{
            synchronized ("B"){
                System.out.println("B线程获取到了B锁");
                synchronized ("A"){
                    System.out.println("B线程获取到了A锁");
                }
            }
        },"B").start();
    }
}
